package com.ycsj.goldmedalnewconcept.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.bean.Classinfo;
import com.ycsj.goldmedalnewconcept.bean.ClassinfoResponse;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//班级列表
@SuppressLint("NewApi")
public class ClassListActivity extends Activity implements OnClickListener {

	private GridView gv_filemanage;
	private FileManageAdapter mAdapter;
	private List<Classinfo> list = new ArrayList<Classinfo>();
	private List<Classinfo> list2 = new ArrayList<Classinfo>();
	private String account;
	private ImageView iv_back;
	private Intent intent;

	private String view_type;// 1.布置作业2、发布活动 3、课程进度4、考勤管理 5、教学与反思 校长端
	// 6、教学与反思 教师端 7、发布活动8、成绩管理 教师端 9.错题集

	private String class_id;
	private String user_id;
	private TextView tv_title;
	private TextView tv_empty;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_classlist);
		intent = getIntent();
		view_type = intent.getStringExtra("view_type");
		user_id = intent.getStringExtra("user_id");
		initView();
		setOnListener();
		initData();
	}

	 int[] classIcons = { R.drawable.class1, R.drawable.class2,
	 R.drawable.class3, R.drawable.class4, R.drawable.class5,
	 R.drawable.class6, R.drawable.class7, R.drawable.class8,
	 R.drawable.class9, R.drawable.class10, };

	private void initView() {
		gv_filemanage = (GridView) findViewById(R.id.gv_filemanage);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
		
		if("1".equals(view_type)){
			tv_title.setText("布置作业");
		}
		if("3".equals(view_type)){
			tv_title.setText("课程进度");
		}
		if("4".equals(view_type)){
			tv_title.setText("考勤管理");
		}
		if("6".equals(view_type)){
			tv_title.setText("教学与反思 ");
		}
		if("8".equals(view_type)){
			tv_title.setText("成绩管理 ");
		}
		if("7".equals(view_type)){
			tv_title.setText("新活动 ");
		}
		if("9".equals(view_type)){
			tv_title.setText("选择班级");
		}

		gv_filemanage.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				if ("1".equals(view_type)) {
					intent = new Intent(getBaseContext(),
							BabyWorkActivity.class);
//					intent = new Intent(getBaseContext(),
//							AssignmentActivity.class);
					class_id = list.get(position).id;
					String num = list.get(position).num;
					String userid = list.get(position).userID;
					intent.putExtra("class_id", num);
					intent.putExtra("ttel", userid);
					intent.putExtra("num", num);
					startActivity(intent);
//					finish();
				} else if ("3".equals(view_type)) {
					intent = new Intent(getBaseContext(),
							CourseProgressActivity.class);
					class_id = list.get(position).num;
					intent.putExtra("class_id", class_id);
					startActivity(intent);
//					finish();

				} else if ("4".equals(view_type)) {
					intent = new Intent(getBaseContext(),
							AttendanceActivity.class);
					class_id = list.get(position).num;
					intent.putExtra("class_id", class_id);
					startActivity(intent);
//					finish();

				} else if ("5".equals(view_type)) {
					intent = new Intent(ClassListActivity.this,
							PrepareLessonsManagerActivity.class);
					class_id = list.get(position).num;
					intent.putExtra("class_id", class_id);
					intent.putExtra("user_id",user_id);
					 startActivity(intent);
					Log.e("test", class_id);
					Log.e("test", user_id);

				} else if ("6".equals(view_type)) {
					intent = new Intent(ClassListActivity.this,
							PrepareLessonsActivity.class);
					class_id = list.get(position).num;
					intent.putExtra("class_id", class_id);
					intent.putExtra("title", "教学与反思");
					startActivity(intent);
				} else if ("7".equals(view_type)) {
					intent = new Intent(ClassListActivity.this,
							HuodongCreatActivity.class);
					class_id = list.get(position).num;
					intent.putExtra("class_id", class_id);
					startActivity(intent);
				}
				else if ("8".equals(view_type)) {
					intent = new Intent(ClassListActivity.this,
							ScoreManageActivity.class);
					class_id = list.get(position).num;
					String num = list.get(position).num;
					
					intent.putExtra("class_id", num);
					intent.putExtra("num", num);
					startActivity(intent);
				}
				else if ("9".equals(view_type)) {
					intent = new Intent(ClassListActivity.this,
							CuotijiActivity.class);
//					class_id = list.get(position).num;
					String num = list.get(position).num;

//					intent.putExtra("class_id", num);
					intent.putExtra("num", num);
					startActivity(intent);
				}
			}
		});
	}

	private void initData() {
		if ("5".equals(view_type)) {
			account = user_id;
		} else {
			account = SPUtil.getString(this, "account", "");
		}

		OkHttpClient mOkHttpClient = new OkHttpClient();
	 // mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "3");
		builder.add("userID", account);
	 // builder.add("userID", "18672626702");

		Request request = new Request.Builder()
				.url(Constant.SERVER_URL + "AppClassMsg").post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", Toast.LENGTH_SHORT)
								.show();
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();

				String json = arg0.body().string();
				ClassinfoResponse fromJson = gson.fromJson(json,
						ClassinfoResponse.class);
				 Log.e("test", json);
				int size = 0;
				if (fromJson != null) {
					if ("-1".equals(fromJson.state)
							|| "0".equals(fromJson.state)) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {

								tv_empty.setVisibility(tv_empty.VISIBLE);
							}
						});
						return;
					}
					if (fromJson.size != "") {
						size = Integer.parseInt(fromJson.size);
					}
				}

				switch (size) {
				case 14:
					list2.add(fromJson.classinfo13);
				case 13:
					list2.add(fromJson.classinfo12);
				case 12:
					list2.add(fromJson.classinfo11);
				case 11:
					list2.add(fromJson.classinfo10);
				case 10:
					list2.add(fromJson.classinfo9);
				case 9:
					list2.add(fromJson.classinfo8);
				case 8:
					list2.add(fromJson.classinfo7);
				case 7:
					list2.add(fromJson.classinfo6);
				case 6:
					list2.add(fromJson.classinfo5);
				case 5:
					list2.add(fromJson.classinfo4);
				case 4:
					list2.add(fromJson.classinfo3);
				case 3:
					list2.add(fromJson.classinfo2);
				case 2:
					list2.add(fromJson.classinfo1);
				case 1:
					list2.add(fromJson.classinfo0);
					for (int i = 0; i < list2.size(); i++) {
						list.add(list2.get(list2.size() - i - 1));
					}
					break;
				default:
					break;
				}

				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mAdapter = new FileManageAdapter(list, getBaseContext());
						gv_filemanage.setAdapter(mAdapter);
					}
				});
			}
		});
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		}
	}

	class FileManageAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private List<Classinfo> list;

		public FileManageAdapter(List<Classinfo> list, Context context) {
			super();
			this.list = list;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			if (null != list) {
				return list.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = inflater
						.inflate(R.layout.gv_classlist_item, null);
				viewHolder = new ViewHolder();
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.name);
				viewHolder.icon = (ImageView) convertView
						.findViewById(R.id.image);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.name.setText(list.get(position).getName());
			 Picasso.with(ClassListActivity.this)
			 .load(classIcons[Integer.parseInt(list.get(position).icon) - 1])
			 .error(R.drawable.header).into(viewHolder.icon);
			return convertView;
		}
	}

	class ViewHolder {
		public TextView name;
		public ImageView icon;

	}
}











